#!/usr/bin/env python3
"""
Simple LangChain Chat Interface Demo
Run this script to test the conversation interface
"""

import os
import sys

from dotenv import load_dotenv

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.services.chat_service import SimpleChatInterface

load_dotenv()

def demo():
    """Demo function to show basic usage"""
    print("🎯 LangChain 对话接口演示")
    print("=" * 50)
    
    # Check for API key
    if not os.getenv("OPENAI_API_KEY"):
        print("⚠️  请设置 OPENAI_API_KEY 环境变量")
        print("示例: export OPENAI_API_KEY='your-api-key-here'")
        return
    
    # Create chat interface
    chat = SimpleChatInterface(model_name="gpt-3.5-turbo")
    
    # Start with system message
    chat.start_conversation("你是一个友好的AI助手，请用中文回答用户问题。")
    
    # Demo conversation
    demo_messages = [
        "你好，请介绍一下你自己",
        "什么是LangChain？",
        "能给我一个简单的Python代码示例吗？"
    ]
    
    print("📝 演示对话开始...")
    print("-" * 30)
    
    for msg in demo_messages:
        print(f"👤 用户: {msg}")
        response = chat.send_message(msg)
        print(f"🤖 助手: {response}")
        print("-" * 30)
    
    # Show chat history
    print("\n📚 对话历史:")
    print("=" * 50)
    for i, message in enumerate(chat.get_chat_history(), 1):
        role = "系统" if message["role"] == "system" else ("用户" if message["role"] == "user" else "助手")
        print(f"{i}. {role}: {message['content']}")
    
    print("\n✅ 演示完成！")


def interactive_mode():
    """Interactive chat mode"""
    print("🗣️  交互式对话模式")
    print("=" * 50)
    
    if not os.getenv("OPENAI_API_KEY"):
        print("⚠️  请设置 OPENAI_API_KEY 环境变量")
        return
    
    chat = SimpleChatInterface()
    chat.run_interactive()


if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "--demo":
        demo()
    else:
        interactive_mode()